#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of_gpio.h>
#include <linux/semaphore.h>
#include <linux/timer.h>
#include <linux/irq.h>
#include <linux/wait.h>
#include <linux/poll.h>
#include <linux/fs.h>
#include <linux/fcntl.h>
#include <linux/platform_device.h>
#include <linux/fcntl.h>
#include <asm/uaccess.h>
#include <asm/io.h>

#define LEDDEV_CNT 1
#define LEDDEV_NAME "led_dev"
#define LED_ON_VALUE 1
#define LED_OFF_VALUE 0

static void __iomem *BUS_IOC_GPIO1A_IOMUX_SEL_L_PI;
static void __iomem *VCCIO1_4_IOC_GPIO1A_DS_L_PI;
static void __iomem *GPIO_SWPORT_DR_L_PI;
static void __iomem *GPIO_SWPORT_DDR_L_PI;

struct ledDev_dev
{
    dev_t devid;
    struct cdev cdev;
    struct class *class;
    struct device *device;
};

static struct ledDev_dev ledDev;

void led_switch(int sta)
{
   

    u32 val = 0;
    if (sta == LED_ON_VALUE)
    {
        val = readl(GPIO_SWPORT_DR_L_PI);
        val &= ~(0X8 << 0);                /* bit3 清零*/
        val |= ((0X8 << 16) | (0X8 << 0)); /* bit19 置 1，允许写 bit3，bit3，高电平*/
        writel(val, GPIO_SWPORT_DR_L_PI);
    }
    else if (sta == LED_OFF_VALUE)
    {
        writel(val, GPIO_SWPORT_DR_L_PI);
        val = readl(GPIO_SWPORT_DR_L_PI);
        val &= ~(0X8 << 0); /* bit3 清零*/
        val |= (0X8 << 16); /* bit19 置 1，允许写 bit3，
        bit3，低电平 */
        writel(val, GPIO_SWPORT_DR_L_PI);
    }
}

void led_unmap(void)
{
    iounmap(BUS_IOC_GPIO1A_IOMUX_SEL_L_PI);
    iounmap(VCCIO1_4_IOC_GPIO1A_DS_L_PI);
    iounmap(GPIO_SWPORT_DR_L_PI);
    iounmap(GPIO_SWPORT_DDR_L_PI);
}

static int led_open(struct inode *inode, struct file *filp)
{
    return 0;
}

static ssize_t led_read(struct file *filp,
                        char __user *buf,
                        size_t cnt,
                        loff_t *offt)
{
    return 0;
}

/*
 * @description : 向设备写数据
 * @param - filp : 设备文件，表示打开的文件描述符
 * @param - buf : 要写给设备写入的数据
 * @param - cnt : 要写入的数据长度
 * @param - offt : 相对于文件首地址的偏移
 * @return : 写入的字节数，如果为负值，表示写入失败
 */
static ssize_t led_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
    int retvalue;
    unsigned char databuf[1];
    unsigned char ledstat;

    retvalue = copy_from_user(databuf, buf, cnt);
    if (retvalue < 0)
    {
        printk(KERN_INFO "kernel write failed!\r\n");
        return -EFAULT;
    }

    ledstat = databuf[0]; /* 获取状态值 */

    if (ledstat == LED_ON_VALUE)
    {
        led_switch(LED_ON_VALUE); /* 打开 LED 灯 */
    }
    else if (ledstat == LED_OFF_VALUE)
    {
        led_switch(LED_OFF_VALUE); /* 关闭 LED 灯 */
    }
    return 0;
}

static struct file_operations led_fops =
    {
        .owner = THIS_MODULE,
        .open = led_open,
        .read = led_read,
        .write = led_write,
};

static int led_prove(struct platform_device *pdev)
{
    int i = 0, ret = 0;
    int ressize[4] = {0};
    u32 val = 0;
    struct resource *ledsource[4];

    printk("led driver and device has matched!\r\n");
    // 获取资源
    for (i = 0; i < 4; i++)
    {
        ledsource[i] = platform_get_resource(pdev, IORESOURCE_MEM, i);
        if (!ledsource[i])
        {
            dev_err(&pdev->dev, "No MEM resource for always on\n");
            return -ENXIO;
        }
        ressize[i] = resource_size(ledsource[i]);
    }
    // 寄存器映射
    BUS_IOC_GPIO1A_IOMUX_SEL_L_PI = ioremap(ledsource[0]->start, ressize[0]);
    VCCIO1_4_IOC_GPIO1A_DS_L_PI = ioremap(ledsource[1]->start, ressize[1]);
    GPIO_SWPORT_DR_L_PI = ioremap(ledsource[2]->start, ressize[2]);
    GPIO_SWPORT_DDR_L_PI = ioremap(ledsource[3]->start, ressize[3]);

    /* 2、设置 GPIO1_A3 为 GPIO 功能。*/
    val = readl(BUS_IOC_GPIO1A_IOMUX_SEL_L_PI);
    val &= ~(0XF000 << 0);                /* bit15:12，清零 */
    val |= ((0XF000 << 16) | (0X0 << 0)); /* bit31:28 置 1，允许写 bit15:12，bit15:12：0，用作 GPIO1_A3 */
    writel(val, BUS_IOC_GPIO1A_IOMUX_SEL_L_PI);
    /* 3、设置 GPIO0_C0 驱动能力为 40ohm */
    val = readl(VCCIO1_4_IOC_GPIO1A_DS_L_PI);
    val &= ~(0X7000 << 0);                   /* bit14:12 清零*/
    val |= ((0X7000 << 16) | (0X6000 << 0)); /* bit30:28 置 1，允许写 bit14:12，192 bit14:12：110， 用作 GPIO1_A3 */
    writel(val, VCCIO1_4_IOC_GPIO1A_DS_L_PI);
    /* 4、设置 GPIO1_A3 为输出 */
    val = readl(GPIO_SWPORT_DDR_L_PI);
    val &= ~(0X8 << 0);                /* bit3 清零*/
    val |= ((0X8 << 16) | (0X8 << 0)); /* bit19 置 1，允许写 bit3，bit3，高电平 */
    writel(val, GPIO_SWPORT_DDR_L_PI);

    /* 5、设置 GPIO1_A3 为低电平，关闭 LED 灯。*/
    val = readl(GPIO_SWPORT_DR_L_PI);
    val &= ~(0X8 << 0); /* bit3 清零*/
    val |= (0X8 << 16); /* bit19 置 1，允许写 bit3，
    bit3，低电平 */
    writel(val, GPIO_SWPORT_DR_L_PI);
    /* 6、注册字符设备驱动 */
    ret = alloc_chrdev_region(&ledDev.devid, 0, LEDDEV_CNT,
                              LEDDEV_NAME);
    if (ret < 0)
    {
        printk(KERN_INFO "register chrdev failed!\r\n");
        goto fail_map;
    }

    // 初始化cdev
    ledDev.cdev.owner = THIS_MODULE;
    cdev_init(&ledDev.cdev, &led_fops);
    ret = cdev_add(&ledDev.cdev, ledDev.devid, LEDDEV_CNT);
    if (ret < 0)
    {
        printk(KERN_INFO "cdev_add failed!\r\n");
        goto del_unregister;
    }
    ledDev.class = class_create(THIS_MODULE, LEDDEV_NAME);
    if (IS_ERR(ledDev.class))
    {
        goto del_cdev;
    }
    ledDev.device = device_create(ledDev.class, NULL, ledDev.devid,
                                  NULL, LEDDEV_NAME);
    if (IS_ERR(ledDev.device))
    {
        goto del_class;
    }
    printk("led init ok!\r\n");
    return 0;
del_class:
    class_destroy(ledDev.class);
del_cdev:
    cdev_del(&ledDev.cdev);
del_unregister:
    unregister_chrdev_region(ledDev.devid, LEDDEV_CNT);
fail_map:
    led_unmap();
    return -EIO;
}

static int led_remove(struct platform_device *pdev)
{
    led_unmap();
    device_destroy(ledDev.class, ledDev.devid);
    class_destroy(ledDev.class);
    cdev_del(&ledDev.cdev);
    unregister_chrdev_region(ledDev.devid, LEDDEV_CNT);
    printk("led remove!\r\n");
    return 0; 
}

static struct platform_driver led_driver =
    {
        .probe = led_prove,
        .remove = led_remove,
        .driver =
            {
                .name = "jack-led",
            },
};

static int __init led_init(void)
{
    return platform_driver_register(&led_driver);
}

static void __exit led_exit(void)
{
    platform_driver_unregister(&led_driver);
}

module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("JACK");
MODULE_INFO(intree, "Y");
